package cj.web.point.controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.admin.utils.DateUtils;
import cj.web.login.controllers.BaseController;
import cj.web.point.domain.PointConfig;
import cj.web.point.service.PointConfigService;
import cj.web.yc.domain.Category;
import cj.web.yc.domain.Video;
import cj.web.yc.service.CategoryService;

@Path("/pointConfig")
public class PointConfigController extends BaseController{

	private static Logger logger = Logger.getLogger(PointConfigController.class.getName());
	
	@Autowired
	private PointConfigService pointConfigService;
	
	@Autowired
	private CategoryService categoryService;
	
	@Get("show")
	public String show(Model model) {
		return "/views/yc/pointConfigList.vm";
	}
	
	@Get("getTree")
	public String getTree(@Param("categoryIds") String categoryIds) {
		JSONObject jo = new JSONObject();

		List<Category> categorylis = categoryService.queryAll();
		List<Map> ret = new ArrayList<Map>();
		for (Category category : categorylis) {
			Map map = new HashMap();
			map.put("id", category.getId());
			map.put("pId", category.getpId());
			map.put("name", category.getName());
			map.put("descs", category.getDescs());
			map.put("open", true);

			if (categoryIds != null 
					&& categoryIds.length() > 0 
					&& (","+categoryIds).indexOf(","+category.getId()) != -1) {
				map.put("checked", true);
			} else {
				map.put("checked", false);
			}
			ret.add(map);
		}

		jo.put("ret", ret);

		return "@" + jo;
	}

	@Post("getList")
	public String queryList(PointConfig pointConfig) {
		//当前页  
		pointConfig.setStart((pointConfig.getPage()-1)*pointConfig.getRows());
		pointConfig.setLength(pointConfig.getRows());
		
		JSONObject jo = new JSONObject();
		
		Map rep = pointConfigService.query(pointConfig);
		List<Video> objs = (List<Video>) rep.get("pointConfiglis");
		int size = (int) rep.get("size");
		
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage() {
		return "/views/yc/pointConfigDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,PointConfig pointConfig,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		pointConfig.setDeleteFlag(Constants.deleteFlag.deleteFlag_1);
		pointConfig.setCreateAt(DateUtils.getNowTime());
		pointConfig.setCreateUser(returnSessionObj(request).getAccount());
		pointConfig.setUpdateAt(DateUtils.getNowTime());
		pointConfig.setUpdateUser(returnSessionObj(request).getAccount());
		
		try{
			pointConfigService.create(pointConfig);
		}catch(Exception e){
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id) {
		PointConfig pointConfig = pointConfigService.queryById(id);

		model.add("pointConfig", pointConfig);
		return "/views/yc/pointConfigEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,PointConfig pointConfig,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		pointConfig.setUpdateAt(DateUtils.getNowTime());
		pointConfig.setUpdateUser(returnSessionObj(request).getAccount());
		
		try {
			pointConfigService.update(pointConfig);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			pointConfigService.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
}
